package com.jyxd.web.task;

import com.jyxd.web.dao.patient.PatientDao;
import com.jyxd.web.data.basic.QuartzTask;
import com.jyxd.web.data.basic.QuartzTime;
import com.jyxd.web.data.basic.VitalSignMerge;
import com.jyxd.web.data.patient.Patient;
import com.jyxd.web.service.assessment.basic.QuartzTaskService;
import com.jyxd.web.service.assessment.basic.QuartzTimeService;
import com.jyxd.web.service.assessment.basic.VitalSignMergeService;
import com.jyxd.web.service.hisview.HisViewService;
import com.jyxd.web.service.patient.PatientService;
import com.jyxd.web.util.CompareTimeUtil;
import com.jyxd.web.util.SpringUtil;
import com.jyxd.web.util.UUIDUtil;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 同步旧表的生命体征  PrimaryCare
 */
public class OldPatientDataJob implements Job {
    private static Logger logger = LoggerFactory.getLogger(OldPatientDataJob.class);
    //定义一个成员变量
    private static QuartzTask quartzTask;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QuartzTime quartzTime = new QuartzTime();
        quartzTime.setStartTime(new Date());
        ServletContext context = null;
        logger.info("开始同步旧表的生命体征");
        CronExpression expression = null;
        try {
            //获取service
            context = (ServletContext) jobExecutionContext.getScheduler().getContext().get(QuartzServletContextListener.MY_CONTEXT_NAME);
            WebApplicationContext cxt = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            QuartzTimeService quartzTimeService = (QuartzTimeService) cxt.getBean("quartzTimeService");
            QuartzTaskService quartzTaskService = (QuartzTaskService) cxt.getBean("quartzTaskService");
            VitalSignMergeService vitalSignMergeService = SpringUtil.getBean(VitalSignMergeService.class);
            PatientService patientService = SpringUtil.getBean(PatientService.class);
            PatientDao patientDao = SpringUtil.getBean(PatientDao.class);
            HisViewService hisViewService = SpringUtil.getBean(HisViewService.class);
            Map<String,Object> map=new HashMap<>();
            map.put("status",1);
            //查询本地的患者数据
            List<VitalSignMerge> vitalSignMerges = vitalSignMergeService.queryList(map);
            if(vitalSignMerges!=null && vitalSignMerges.size()>0) {
                //如果HIS数据库有数据更新，没有数据新增
                for (VitalSignMerge vitalSignMerge : vitalSignMerges) {
                    Patient patient = patientDao.queryData(vitalSignMerge.getPatientId());
                    //赋值病重（病危）患者护理记录
                    hisViewService.saveDcHlczjlBzbwhlHlgc(patient, vitalSignMerge);
                    //新增生命体征记录表和记录值
                    hisViewService.saveDcIcuDatTzjlOrDcIcuDatTzvalue(patient, vitalSignMerge);
                    //护理记录增改
                    hisViewService.saveDcHlczjlBzbwhl(patient, vitalSignMerge);
                }
            }
            //操作定时任务执行记录表
            Map<String, Object> newMap = new HashMap<>();
            map.put("jobName", "OldPatientDataJob");
            map.put("jobGroup", "OldPatientDataJob");
            quartzTask = quartzTaskService.queryDataByNameAndGroup(newMap);
            expression = new CronExpression(quartzTask.getCron());
            quartzTime.setNextTime(expression.getNextValidTimeAfter(quartzTime.getStartTime()));
            quartzTime.setQuartzTaskId(quartzTask.getId());
            quartzTime.setId(UUIDUtil.getUUID());
            quartzTime.setEndTime(new Date());
            quartzTime.setTimeLength(String.valueOf(CompareTimeUtil.calculatetimeGapSecond(quartzTime.getStartTime(), quartzTime.getEndTime())));
            quartzTime.setStatus(1);
            quartzTimeService.insert(quartzTime);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

